/**
 * 
 */
package com.shy.plugin.seo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.shy.base.util.StringUtils;
import com.shy.cms.entity.Site;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.http.HttpUtil;

/**
 * <p>Title:BaiduPost
 * <p>Description:
 * @author H2013788
 * @date 2019年8月30日
 */
public class BaiduPost {

	private static String zzApiUrl = "http://data.zz.baidu.com/urls?"; //从百度站长平台获取
	
    /**
     * baidu post
     * @param urls  链接列表
     * @param params 参数
     * @return
     * @throws IOException   
     * @date: 2020年3月4日 下午9:37:14
     */
    public static String postUrl(List<String> urls, Map<String, Object> params) throws IOException {
		Set<String> set = new HashSet<String>(urls);
		urls = new ArrayList<String>(set);
		Site site = (Site) params.get("site");
		String result = "";
		if (StringUtils.isNotEmpty(site.getmBaseUrl())) {
			// 有移动端时拆分提交
			List<String> pcList = new ArrayList<String>();
			List<String> mList = new ArrayList<String>();
			for (String url : urls) {
				if (url.indexOf(site.getmBaseUrl()) >= 0) {
					mList.add(url);
				} else {
					pcList.add(url);
				}
			}
			if(!pcList.isEmpty()) {
				params.put("urlType", "pc");
				result += "【pc提交】：" + postUrlByUrlType(pcList, params);
			}
			if(!mList.isEmpty()) {
				params.put("urlType", "m");
				result += "【m提交】：" + postUrlByUrlType(mList, params);
			}
		} else {
			params.put("urlType", "pc");
			result += postUrlByUrlType(urls, params);
		}
		return result;
    }
    
    
    /**
     * baidu post
     * @param urls  链接列表
     * @param params 参数
     * @return
     * @throws IOException   
     * @date: 2020年3月4日 下午9:37:14
     */
    public static String postUrlByUrlType(List<String> urls, Map<String, Object> params) throws IOException {
        Set<String> set = new HashSet<String>(urls);
        urls = new ArrayList<String>(set);
    	Site site = (Site) params.get("site");
    	StringBuffer url = new StringBuffer();
    	url.append(zzApiUrl);
		//默认pc端
        String host = site.getBaseUrl();
        //如果是m端
        if(params.containsKey("urlType") && "m".equals(params.get("urlType").toString()) 
                && StringUtils.isNotEmpty(site.getmBaseUrl())) {
            host = site.getmBaseUrl();
        }
        if(host.startsWith("http://")) {
            host = host.substring(7);
        }
        return postBaidu(urls, host, site.getBaiduToken());
    }
    
    
    /**
     * baidu post
     * @param urls  链接列表
     * @param host  域名，自行判断是否http开头
     * @param token 百度Token
     * @return
     * @throws IOException   
     * @date: 2020年3月4日 下午9:37:14
     */
    public static String postBaidu(List<String> urls, String host, String token) {
        String result = "";
        Set<String> set = new HashSet<String>(urls);
        urls = new ArrayList<String>(set);
        try {
            // https://开头的保留，http://开头的只保留域名
            if(host.startsWith("http://")) {
                host = host.substring(7);
            }
            StringBuffer url = new StringBuffer();
            url.append(zzApiUrl);
            url.append("site=" + host);
            url.append("&token=" + token);
            result = HttpUtil.post(url.toString(), CollUtil.join(urls, "\n"));
        } catch (Exception e) {
            
        }
        return result;
    }
    
    
    public static void main(String[] args) {
        List<String> urls = new ArrayList<String>();
        urls.add("http://m.2itcn.com/news/221108/pkikuy16.html");
        urls.add("http://m.2itcn.com/news/221108/zibipf1g.html");
        urls.add("http://m.2itcn.com/news/221108/ygk7pidi.html");
        postBaidu(urls, "http://m.2itcn.com", "tkBrMFJBlIblHHKs");
    }
    
}
